package com.ky.tms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ky.tms.entity.*;
import com.ky.tms.exception.PredictableException;
import com.ky.tms.mapper.TmsLineMapper;
import com.ky.tms.mapper.TmsLinePriceMapper;
import com.ky.tms.service.ITmsLineService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ky.tms.util.DateUtil;
import com.ky.tms.util.RequestContextHolderUtil;
import com.ky.tms.vo.LinePriceVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author chengkaimao
 * @since 2020-06-05
 */
@Service
public class TmsLineServiceImpl extends ServiceImpl<TmsLineMapper, TmsLine> implements ITmsLineService {

    @Autowired
    private TmsLineMapper tmsLineMapper;

    @Autowired
    private TmsLinePriceMapper tmsLinePriceMapper;

    @Override
    public IPage<TmsLine> page(TmsLine tmsLine, Long limit, Long pages) {
        IPage<TmsLine> page = new Page(pages, limit);
        QueryWrapper<TmsLine> wrapper = new QueryWrapper<>();
        if(StringUtils.isNotEmpty(tmsLine.getCustomerName())){
            wrapper.like("customer_name", tmsLine.getCustomerName());
        }
        if(StringUtils.isNotEmpty(tmsLine.getLineStart())){
            wrapper.like("line_start", tmsLine.getLineStart());
        }
        if(StringUtils.isNotEmpty(tmsLine.getLineEnd())){
            wrapper.like("line_end", tmsLine.getLineEnd());
        }
        wrapper.eq("is_valid", 1);
        wrapper.orderByDesc("create_time");
        page = this.page(page, wrapper);
        return page;
    }

    @Override
    public void add(TmsLine tmsLine) throws PredictableException {
        String userName = RequestContextHolderUtil.getUserName();
        tmsLine.setIsLock(0);
        tmsLine.setIsValid(1);
        tmsLine.setCreateTime(LocalDateTime.now());
        tmsLine.setCreateUser(userName);
        tmsLineMapper.insert(tmsLine);
    }

    @Override
    public void delete(List<Long> ids) {
        String userName = RequestContextHolderUtil.getUserName();
        tmsLineMapper.deleteByIds(userName, LocalDateTime.now(), ids);
    }

    @Override
    public void update(TmsLine tmsLine) throws PredictableException {
        String userName = RequestContextHolderUtil.getUserName();
        tmsLine.setUpdateTime(LocalDateTime.now());
        tmsLine.setUpdateUser(userName);
        tmsLineMapper.updateById(tmsLine);
    }

    @Override
    public List<LinePriceVo> selectLinePrice(Long lineId) {
        return tmsLinePriceMapper.selectLinePrice(lineId);
    }

    @Override
    public List<Map<String, Object>> selectList(Long customerId) {
        return tmsLineMapper.selectOption(customerId);
    }
}
